package org.springblade.modules.auth.utils;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class HttpClientUtils {

	final static int TIMEOUT = 1000;

	final static int TIMEOUT_MSEC = 5 * 1000;

	public static String doPost(String url, Map<String, String> paramMap) throws IOException {
		// 创建Httpclient对象
		CloseableHttpClient httpClient = HttpClients.createDefault();
		CloseableHttpResponse response = null;
		String resultString = "";

		try {
			// 创建Http Post请求
			HttpPost httpPost = new HttpPost(url);

			// 创建参数列表
			if (paramMap != null) {
				List<NameValuePair> paramList = new ArrayList<>();
				for (Map.Entry<String, String> param : paramMap.entrySet()) {
					paramList.add(new BasicNameValuePair(param.getKey(), param.getValue()));
				}
				// 模拟表单
				UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList);
				httpPost.setEntity(entity);
			}

			httpPost.setConfig(builderRequestConfig());

			// 执行http请求
			response = httpClient.execute(httpPost);

			resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
		} catch (Exception e) {
			throw e;
		} finally {
			try {
				response.close();
			} catch (IOException e) {
				throw e;
			}
		}

		return resultString;
	}

	private static RequestConfig builderRequestConfig() {
		return RequestConfig.custom()
			.setConnectTimeout(TIMEOUT_MSEC)
			.setConnectionRequestTimeout(TIMEOUT_MSEC)
			.setSocketTimeout(TIMEOUT_MSEC).build();
	}
	public static String get(String param,String URL) {
		CloseableHttpClient httpclient = HttpClients.createDefault();
		String obj=null;
		try {
			HttpUriRequest httpget = new HttpGet(URL+"?"+param);
			// 执行get请求.
			CloseableHttpResponse response = httpclient.execute(httpget);
			try {
				// 获取响应实体
				HttpEntity entity = response.getEntity();
				// 打印响应状态
				System.out.println(response.getStatusLine());
				if (entity != null) {
					obj=EntityUtils.toString(entity);
				}
			} finally {
				response.close();
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			// 关闭连接,释放资源
			try {
				httpclient.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return obj;
	}
}
